import csv
from django.db import transaction
from lotto.catalog.models import *


@transaction.commit_manually        
def load_lotto ():
    reader = csv.reader (open('db/wyniki-lotto.csv'), delimiter = ';');
    try:
        for r in reader:
            l = Lotto (date = '%s-%s-%s' % (r[3], r[2], r[1]),
                       n1 = r[4], n2 = r[5], n3 = r[6],
                       n4 = r[7], n5 = r[8], n6 = r[9]);
            l.save ();
    except: 
        transaction.rollback ();
        return 'Lotto - nastapil blad podczas importowania danych';
    else:
        transaction.commit ();
        return 'Lotto - dane zostaly poprawnie zaimportowane';


@transaction.commit_manually        
def load_mini ():
    reader = csv.reader (open('db/wyniki-minilotto.csv'), delimiter = ';');
    try:
        for r in reader:
            m = MiniLotto (date = '%s-%s-%s' % (r[3], r[2], r[1]),
                           n1 = r[4], n2 = r[5], n3 = r[6],
                           n4 = r[7], n5 = r[8]);
            m.save ();
    except: 
        transaction.rollback ();
        return 'MiniLotto - nastapil blad podczas importowania danych';
    else:
        transaction.commit ();
        return 'MiniLotto - dane zostaly poprawnie zaimportowane';


@transaction.commit_manually        
def load_multi ():
    reader = csv.reader (open('db/wyniki-multimulti.csv'), delimiter = ';');
    try:
        for r in reader:
            m = MultiMulti (date = '%s-%s-%s' % (r[3], r[2], r[1]),
                            n1 = r[4], n2 = r[5], n3 = r[6],
                            n4 = r[7], n5 = r[8], n6 = r[9],
                            n7 = r[10], n8 = r[11], n9 = r[12],
                            n10 = r[13], n11 = r[14], n12 = r[15],
                            n13 = r[16], n14 = r[17], n15 = r[18],
                            n16 = r[19], n17 = r[20], n18 = r[21],
                            n19 = r[22], pls = r[23]);
            m.save ();
    except: 
        transaction.rollback ();
        return 'MultiMulti - nastapil blad podczas importowania danych';
    else:
        transaction.commit ();
        return 'MultiMulti - dane zostaly poprawnie zaimportowane';


@transaction.commit_manually        
def load_joker ():
    reader = csv.reader (open('db/wyniki-joker.csv'), delimiter = ';');
    try:
        for r in reader:
            m = Joker (date = '%s-%s-%s' % (r[3], r[2], r[1]),
                           jr = r[4], n1 = r[5], n2 = r[6],
                           n3 = r[7], n4 = r[8]);
            m.save ();
    except: 
        transaction.rollback ();
        return 'Joker - nastapil blad podczas importowania danych';
    else:
        transaction.commit ();
        return 'Joker - dane zostaly poprawnie zaimportowane';


@transaction.commit_manually        
def load_kaskada ():
    reader = csv.reader (open('db/wyniki-kaskada.csv'), delimiter = ';');
    try:
        for r in reader:
            m = Kaskada (date = '%s-%s-%s' % (r[3], r[2], r[1]),
                         n1 = r[4], n2 = r[5], n3 = r[6],
                         n4 = r[7], n5 = r[8], n6 = r[9],
                         n7 = r[10], n8 = r[11], n9 = r[12],
                         n10 = r[13], n11 = r[14], n12 = r[15]);
            m.save ();
    except: 
        transaction.rollback ();
        return 'Kaskada - nastapil blad podczas importowania danych';
    else:
        transaction.commit ();
        return 'Kaskada - dane zostaly poprawnie zaimportowane';

